package com.example.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.server.mapper.UserMapper;
import com.example.server.mapper.entity.User;
import com.example.server.service.commands.RegisterCommand;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserService {
    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public void create(RegisterCommand registerCommand) {
        User user = new User();
        user.setUserName(registerCommand.getUserName());
        user.setPassword(registerCommand.getPassword());
        user.setCreatedAt(LocalDateTime.now());
        user.setUpdatedAt(LocalDateTime.now());
        userMapper.insert(user);
    }

    public List<User> getList() {
        return userMapper.selectList(null);
    }

    public User getOne(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserName, username);
        return userMapper.selectOne(queryWrapper);
    }

}
